/**************************************************************************
*  Black Knight - Chess Engine                                            *
*  Copyright (C) 2009  Victor Rusu                                        *
*                                                                         *
*  This program is free software: you can redistribute it and/or modify   *
*  it under the terms of the GNU General Public License as published by   *
*  the Free Software Foundation, either version 3 of the License, or      *
*  (at your option) any later version.                                    *
*                                                                         *
*  This program is distributed in the hope that it will be useful,        *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           *
*  GNU General Public License for more details.                           *
*                                                                         *
*  You should have received a copy of the GNU General Public License      *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
**************************************************************************/

#include "io/interface.h"

#include <string>
#include <boost/thread/mutex.hpp>
#include "io/read.h"
#include "io/write.h"
#include "io/xboard.h"

Interface* Interface::interface_instance_ = NULL;
boost::mutex Interface::instance_lock_;

Interface::Interface() {
}

Interface::~Interface() {
}

Interface* Interface::instance() {
    instance_lock_.lock();
    if (interface_instance_ == NULL) {
        interface_instance_ = new Interface;
    }
    instance_lock_.unlock();

    return interface_instance_;
}

void Interface::operator()() {
    std::string command;

    while (1) {
        command = Read::instance()->get_command();
        Xboard::instance()->lock();
        Xboard::instance()->receive_command(command);
        Xboard::instance()->unlock();
    }
}
